import request from '@/utils/request'
import type R from '@/types/conduit/R'
import type {
  DeliveryOrderDetailsVO,
  DinersForMap,
  NearbyDeliveryOrder,
  ShopForMap,
  WaitPickingDeliveryOrder
} from '@/types/response/delivery-order'
import type { PageResponse } from '@/types/generic/PageResponse'
import type { NearbyDeliveryOrderQueryParams } from '@/types/param/delivery-order'
import type { Page } from '@/types/generic/Page'
import type DeliveryOrder from '@/types/entity/DeliveryOrder'
import type GeoCoordinates from '@/types/generic/GeoCoordinates'
import type { GEOObj } from '@/store/modules/common/geo'
import type CompletedQuantityStatistics from '@/types/response/delivery-order'

export const nearbyDeliveryOrders = (
  params: NearbyDeliveryOrderQueryParams
): Promise<R<PageResponse<NearbyDeliveryOrder>>> =>
  request({
    url: `/rider/delivery-order/nearby-delivery-orders`,
    method: 'GET',
    params
  })
export const riderWaitPickingPage = (params: Page): Promise<R<PageResponse<WaitPickingDeliveryOrder>>> =>
  request({
    url: `/rider/delivery-order/rider-wait-picking-page`,
    method: 'GET',
    params
  })
export const acceptSystemDispatchDeliveryOrder = (deliveryOrderId: number): Promise<R<null>> =>
  request({
    url: `/rider/delivery-order/accept-system-dispatch-delivery-order/${deliveryOrderId}`,
    method: 'POST'
  })
export const tryGrabbingOrder = (deliveryOrderId: number): Promise<R<null>> =>
  request({
    url: `/rider/delivery-order/try-grabbing-order/${deliveryOrderId}`,
    method: 'POST'
  })
export const reportToShop = (deliveryOrderId: number): Promise<R<number | undefined>> =>
  request({
    url: `/rider/delivery-order/report-to-shop/${deliveryOrderId}`,
    method: 'POST'
  })

export const confirmMealPickup = (deliveryOrderId: number): Promise<R<null>> =>
  request({
    url: `/rider/delivery-order/confirm-meal-pickup/${deliveryOrderId}`,
    method: 'POST'
  })
export const riderCompleteDelivery = (deliveryOrderId: number): Promise<R<null>> =>
  request({
    url: `/rider/delivery-order/rider-complete-delivery/${deliveryOrderId}`,
    method: 'POST'
  })
export const riderWaitDelivery = (params: Page): Promise<R<PageResponse<DeliveryOrder>>> =>
  request({
    url: `/rider/delivery-order/rider-wait-delivery-page`,
    method: 'GET',
    params
  })
export const riderRoutePlanning = (params: { longitude: number; latitude: number }): Promise<R<ShopForMap[]>> =>
  request({
    url: `/rider/delivery-order/rider-route-planning`,
    method: 'GET',
    params
  })
export const deliveryOrderDetail = (
  deliveryOrderId: string,
  ridderGeo: GeoCoordinates
): Promise<R<DeliveryOrderDetailsVO>> =>
  request({
    url: `/rider/delivery-order/delivery-order-detail/${deliveryOrderId}`,
    method: 'GET',
    params: ridderGeo
  })

/**
 * 查询一段时间内完成的量统计
 */
export const getCompletedQuantityStatistics = (
  startTime?: string,
  endTime?: string
): Promise<R<CompletedQuantityStatistics>> =>
  request({
    url: `/rider/delivery-order/completed-quantity-statistics`,
    method: 'GET',
    params: { startTime, endTime }
  })
